<?php

/**
 *  常用模型类
 * @authors Hex (hex@zpdchina.com)
 * @date    2017-03-08 15:26:18
 * @version $Id$
 */
class CommonModel extends BaseModel
{
    /**
     * 插入一条数据
     * @param array $items
     * @return bool|int
     */
    public static function addData($table_name, $insert_data)
    {
        $insert_id = DB::insert($table_name, array_keys($insert_data))
            ->values(array_values($insert_data))
            ->execute();
        return $insert_id;
    }

    /**
     * 更新记录
     * @param array $items
     * @return bool|int
     */
    public static function updateRows($table = '', $setArr = array(), $colArr = array(), $valArr = array())
    {
        if (empty($valArr)) {
            return false;
        }
        $query = DB::update($table)->set($setArr);
        for ($i = 0; $i < count($colArr); $i++) {
            if (strstr($valArr[$i], ','))
                $query->where($colArr[$i], 'IN', explode(',', $valArr[$i]));
            else if (is_array($valArr[$i]))
                $query->where($colArr[$i], 'IN', $valArr[$i]);
            else
                $query->where($colArr[$i], '=', $valArr[$i]);
        }
        $affect = $query->execute();
        if ($affect)
            return true;
    }

    /**
     * 删除指定记录
     * @param $id
     * @return bool
     */
    public static function delelteRows($table = '', $colArr = array(), $valArr = array())
    {
        if (empty($valArr)) return false;
        $query = DB::delete($table);
        for ($i = 0; $i < count($colArr); $i++) {
            if (strstr($valArr[$i], ','))
                $query->where($colArr[$i], 'IN', explode(',', $valArr[$i]));
            else if (is_array($valArr[$i]))
                $query->where($colArr[$i], 'IN', $valArr[$i]);
            else
                $query->where($colArr[$i], '=', $valArr[$i]);
        }
        $affect = $query->execute();
        if ($affect)
            return true;

    }


    /**
     * 根据条件获取对应的病人信息
     * @param  $user_department
     * @return $zhuyuan_basic_info_list
     */
    public static function getZhuyuanIdList($fields = '', $user_department, $yiyuan_id, $modify_time = '')
    {
        $fields = (empty($fields) ? 't.*,a.*' : $fields);
        $sql_where = " 1=1";
        if ($yiyuan_id) {
            $sql_where .= " and t.yiyuan_id = '" . $yiyuan_id . "'";
        }
        if ($user_department) {
            $sql_where .= " and a.shenfenzheng_hao = '" . $user_department . "'";
        }
        if ($modify_time) {
            $sql_where .= " and t.modify_time>='$modify_time'";
        }
        $huanzhe_chuyuan_baoliu_shijian = Yaconf::get("config$yiyuan_id.huanzhe_chuyuan_baoliu_shijian");
        if (isset($huanzhe_chuyuan_baoliu_shijian)) {
            $sql_where .= " and (t.zhuangtai = '住院中' or (t.zhuangtai = '已出院' and " . Utility::getIntervalSqlString("t.chuyuan_riqi_time", $huanzhe_chuyuan_baoliu_shijian) . "))";
        } else {
            $sql_where .= " and t.zhuangtai = '住院中'";
        }
        $order = " order by t.zhuangtai asc ,(t.bingchuang_hao+0) ASC";
        $sql = "SELECT $fields FROM zhuyuan_basic_info t
                LEFT JOIN patient_basic_info a on t.patient_id=a.patient_id and t.yiyuan_id=a.yiyuan_id";
        if (strstr($fields, 'b.'))
            $sql .= " LEFT JOIN patient_contact_info b on a.patient_id=b.patient_id and t.yiyuan_id = b.yiyuan_id";
        $sql .= " where" . $sql_where . $order;
        $patient_list = DB::query(Database::SELECT, $sql)->execute()->as_array();
        return $patient_list;
    }

    //获取住院病人zhuyuan_id
    public static function getZhuyuanIds($user_department, $yiyuan_id, $zhuyuan_id = '')
    {
        $zhuayun_bingren = array();
        if (empty($zhuyuan_id)) {
            $zhuyuan_basic_info = DB::select('zhuyuan_id')
                ->from('zhuyuan_basic_info')
                ->where('zhuyuan_bingqu', '=', $user_department)
                ->where('zhuangtai', '=', '住院中')
                ->where('yiyuan_id', '=', $yiyuan_id)
                ->execute()->as_array();
            $zhuyuan_bingren = array();
            foreach ($zhuyuan_basic_info as $one) {
                if (!in_array($one['zhuyuan_id'], $zhuayun_bingren))  //数组$zhuayun_bingren中没有匹配的$one['zhuyuan_id']
                    $zhuyuan_bingren[] = $one['zhuyuan_id'];
            }
        } else {
            $zhuyuan_bingren[] = $zhuyuan_id;
        }
        return $zhuyuan_bingren;
    }
}
